package ui.demo;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Cube {
	
	// Properties
	private Vec3 size;
	private Vec3 position;
	private Vec3 rotation;
	private float[][] vertices;
	// Buffers
	private FloatBuffer[] vertexBuffer;
	
	public void setPosition(Vec3 position) {
		this.position = position;
	}
	public Vec3 getPosition() {
		return position;
	}
	
	public void setRotation(Vec3 rotation) {
		this.rotation = rotation;
	}
	public Vec3 getRotation() {
		return rotation;
	}
	
	// Default constructor
	public Cube() {
		this(new Vec3(1,1,1),new Vec3(0, 0, -5),new Vec3(0,0,0));
		setVertices();
		setBuffers();
	}
	
	// Custom constructor
	public Cube(Vec3 size, Vec3 position, Vec3 rotation) {
		this.size = size;
		this.position = position;
		this.rotation = rotation;
		setVertices();
		setBuffers();
	}
	
	private float[] cubeColors = new float[] {
		0,1,0,1,
		1,0.5f,0,1,
		1,0,0,1,
		1,1,0,1,
		0,0,1,1,
		1,0,1,1		
	};
	
	public void draw(GL10 gl) {
		// draw cube
		gl.glLoadIdentity();
		gl.glTranslatef(position.x, position.y, position.z);
		gl.glRotatef(rotation.x, 1, 0, 0);
		gl.glRotatef(rotation.y, 0, 1, 0);
		gl.glRotatef(rotation.z, 0, 0, 1);
		for (int i = 0; i < 6; i++)
		{
			gl.glColor4f(cubeColors[4*i+0], cubeColors[4*i+1], cubeColors[4*i+2], cubeColors[4*i+3]);
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer[i]);
			gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		}
		
	}
	
	private void setVertices() {
		vertices = new float[][] {
				new float[] { // top
					 size.x, size.y ,-size.z,
					-size.x, size.y ,-size.z,
					-size.x, size.y , size.z,
					 size.x, size.y , size.z
				},
				new float[] { // bottom
					 size.x,-size.y , size.z,
					-size.x,-size.y , size.z,
					-size.x,-size.y ,-size.z,
					 size.x,-size.y ,-size.z
				},
				new float[] { // front
					 size.x, size.y , size.z,
					-size.x, size.y , size.z,
					-size.x,-size.y , size.z,
					 size.x,-size.y , size.z
				},
				new float[] { // back
					 size.x,-size.y ,-size.z,
					-size.x,-size.y ,-size.z,
					-size.x, size.y ,-size.z,
					 size.x, size.y ,-size.z
				},
				new float[] { // left
					-size.x, size.y , size.z,
					-size.x, size.y ,-size.z,
					-size.x,-size.y ,-size.z,
					-size.x,-size.y , size.z
				},
				new float[] { // right
					 size.x, size.y ,-size.z,
					 size.x, size.y , size.z,
					 size.x,-size.y , size.z,
					 size.x,-size.y ,-size.z
				},
		};
	}
	
	private void setBuffers() {
		ByteBuffer bb;
		vertexBuffer = new FloatBuffer[6];
		for (int i = 0; i < 6; i++)
		{
			bb = ByteBuffer.allocateDirect(vertices[i].length * 4);
			bb.order(ByteOrder.nativeOrder());
			vertexBuffer[i] = bb.asFloatBuffer();
			vertexBuffer[i].put(vertices[i]);
			vertexBuffer[i].position(0);
		}
	}

}
